package com.tensquare.sms.consumer;

import com.aliyuncs.exceptions.ClientException;
import com.tensquare.util.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 发送短信的消息消费者
 */
@Component
@RabbitListener(queues = "tensqureusersms")
public class SmsConsumer {

    @Autowired
    private SmsUtil smsUtil;

    @Value("${aliyun.sms.template_code}")
    private String template_code;

    @Value("${aliyun.sms.sign_name}")
    private String sign_name;

    @RabbitHandler
    public void sms(Map map){
        System.out.println("发送短信啦："+map);
        String num = (String) map.get("num");
        String mobile = (String) map.get("mobile");

        //参数：
        String param = "{\"code\":\""+num+"\"}";

        try {
            smsUtil.sendSms(mobile,template_code,sign_name,param);
        } catch (ClientException e) {
            e.printStackTrace();
            System.out.println("发送短信失败！");
        }
    }
}
